package com.contractpage.sys.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

import com.alibaba.druid.pool.DruidDataSource;
import com.common.listener.MyApplicationContextAware;
import com.common.seq.sql.SqlSeqUtil;

@SuppressWarnings("rawtypes")
public class StartAfterListener implements ApplicationListener {
	private Logger log = LoggerFactory.getLogger(StartAfterListener.class);

	/** 防止重复执行 */
	private static boolean isStart = false;

	public void onApplicationEvent(ApplicationEvent arg0) {
		if (isStart) {
			return;
		}
		isStart = true;

		if (log.isDebugEnabled()) {
			log.debug("StartAfterListener onApplicationEvent start...");
		}


		try {
			// 初始化序列生成器
			initSetSeq();
		} catch (Exception e) {
			log.error("", e);
		}
	}
	/**
	 *
	 * 初始化序列生成器
	 *
	 */
	private void initSetSeq() {
		ApplicationContext context = MyApplicationContextAware.getContext();
		DruidDataSource dataSource = (DruidDataSource) context.getBean("dataSource");

		log.info(" 主键序列号生成器初始化开始--------------");
		// 主键序列号生成器初始化
		SqlSeqUtil.initSqlSeq(dataSource);
		log.info(" 主键序列号生成器初始化结束--------------");
	}
}